package com.juneng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.juneng.common.Condition;
import com.juneng.common.R;
import com.juneng.entity.User;
import com.juneng.mapper.UserMapper;
import com.juneng.utils.BeanCopyUtils;
import com.juneng.entity.CommentDiscuss;
import com.juneng.mapper.CommentDiscussMapper;
import com.juneng.service.ICommentDiscussService;
import com.juneng.dto.CommentDiscussDto;
import com.juneng.dto.CommentDiscussQueryDto;
import com.juneng.vo.CommentDiscussVo;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import java.util.List;
/**
 * 话题评论 服务实现类
 *
 * @author zds
 * @since 2021-07-28
 */
@Service
@AllArgsConstructor
public class CommentDiscussServiceImpl implements ICommentDiscussService {

    protected CommentDiscussMapper commentDiscussMapper;

    protected UserMapper userMapper;

    @Override
    public IPage<CommentDiscussVo> page(CommentDiscussQueryDto dto) {
        IPage<CommentDiscussVo> page = Condition.getPage(dto);
        return commentDiscussMapper.page(page,dto);
    }

    @Override
    public List<CommentDiscuss> list(CommentDiscussQueryDto dto) {
        QueryWrapper<CommentDiscuss> queryWrapper = new QueryWrapper<>(BeanCopyUtils.copy(dto,CommentDiscuss.class));
        return commentDiscussMapper.selectList(queryWrapper);
    }

    @Override
    public R<?> save(CommentDiscussDto dto) {
        //查询用户手机号是否存在
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserId, dto.getUserId()));
        if (user == null){
            return R.failed("用户手机号没有注册");
        }
        dto.setUserId(user.getUserId());
        int insert = commentDiscussMapper.insert(BeanCopyUtils.copy(dto, CommentDiscuss.class));
        return R.success(insert) ;
    }

    @Override
    public Integer updateById(CommentDiscussDto dto) {
        return commentDiscussMapper.updateById(BeanCopyUtils.copy(dto,CommentDiscuss.class));
    }

    @Override
    public Integer deleteLogic(Long id) {
        return commentDiscussMapper.deleteById(id);
    }

    @Override
    public CommentDiscussVo getById(Long id) {
        return commentDiscussMapper.getCommentDiscuss(id);
    }
}
